package com.imooc.utils.ffmpeg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FFMpegDelAudioUtils {


    /**
     * 去除音轨
     *
     * @param ffmpegExePath   ffmpeg路径
     * @param videoFilePath   视频输入路径
     * @param videoFilePath   视频输入路径
     * @param outputVideoPath 视频输出路径
     */
    public String delAudioFromVideo(String ffmpegExePath, String videoFilePath,String audioFilePath, String outputVideoPath) {


        //1.消除原视频的音轨
        List<String> command = new ArrayList<String>();
//        command.add(ffmpegExePath);
//        command.add("-i");
//        command.add(videoFilePath);
//        command.add("-vcodec");
//        command.add("copy");
//        command.add("-an");
//        command.add(outputVideoPath);
        command.add(ffmpegExePath);
        command.add("-i");
        command.add(videoFilePath);
        command.add("-i");
        command.add(audioFilePath);


        command.add("-c:v");
        command.add("copy");
        command.add("-c:a");
        command.add("aac");
        command.add("-strict");
        command.add("experimental");
        command.add("-map");
        command.add("0:v:0");
        command.add("-map");
        command.add("1:a:0");
        command.add(outputVideoPath);
        ProcessBuilder builder = new ProcessBuilder();

        builder.command(command);

        //正常信息和错误信息合并输出
        builder.redirectErrorStream(true);
        try {
            //开始执行命令
            Process process = builder.start();
            //如果你想获取到执行完后的信息，那么下面的代码也是需要的
            StringBuffer sbf = new StringBuffer();
            String line = null;
            InputStream inputStream = process.getErrorStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader br = new BufferedReader(inputStreamReader);
            while ((line = br.readLine()) != null) {
                sbf.append(line);
                sbf.append(" ");
            }
            String resultInfo = sbf.toString();
            System.out.println(resultInfo);
            if (br != null) {
                br.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (inputStreamReader != null) {
                inputStreamReader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outputVideoPath;
    }
    /**
     * @param videoInputPath 原视频的全路径
     * @param audioInputPath 音频的全路径
     * @param videoOutPath 视频与音频结合之后的视频的路径
     * @throws Exception
     */
    public static void convetor( String FFMpegPath,  String videoInputPath, String audioInputPath, double videoSeconds ,String videoOutPath)
            throws Exception {
        Process process = null;
        try {
            String command =FFMpegPath + " -i " + videoInputPath + " -i " + audioInputPath + " -c:v copy -c:a aac -strict experimental " +
                    " -map 0:v:0 -map 1:a:0  -t " + videoSeconds
                    + " -y "
                    + videoOutPath;
            process = Runtime.getRuntime().exec(command);
          //  process.waitFor();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 使用这种方式会在瞬间大量消耗CPU和内存等系统资源，所以这里我们需要对流进行处理
        InputStream errorStream = process.getErrorStream();
        InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = "";
        while ((line = br.readLine()) != null) {
        }
        if (br != null) {
            br.close();
        }
        if (inputStreamReader != null) {
            inputStreamReader.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }

}
